<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入申请人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社团ID" prop="clubId">
        <el-input
          v-model="queryParams.clubId"
          placeholder="请输入社团ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="招聘ID" prop="recruitmentId">
        <el-input
          v-model="queryParams.recruitmentId"
          placeholder="请输入招聘ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社团审核人" prop="clubAuditorId">
        <el-input
          v-model="queryParams.clubAuditorId"
          placeholder="请输入社团审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker clearable
          v-model="queryParams.applyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker clearable
          v-model="queryParams.auditTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['club:application:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['club:application:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['club:application:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['club:application:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="申请ID" align="center" prop="id" /> -->
      <el-table-column label="申请人" align="center" prop="userId">
        <template slot-scope="scope">
          <span>{{ getUserName(scope.row.userId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="社团" align="center" prop="clubId">
        <template slot-scope="scope">
          <span>{{ getClubName(scope.row.clubId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="招聘ID" align="center" prop="recruitmentId" />
      <el-table-column label="简历内容" align="center" prop="resumeContent" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="statusTagType(scope.row.status)">{{ statusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="社团审核人" align="center" prop="clubAuditorId" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['club:application:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            v-if="scope.row.status !== 1"
            @click="changeStatus(scope.row, 1)"
            v-hasPermi="['club:application:edit']"
          >允许</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            v-if="scope.row.status !== 2"
            @click="changeStatus(scope.row, 2)"
            v-hasPermi="['club:application:edit']"
          >拒绝</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['club:application:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社团报名申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请人ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入申请人ID" />
        </el-form-item>
        <el-form-item label="社团ID" prop="clubId">
          <el-input v-model="form.clubId" placeholder="请输入社团ID" />
        </el-form-item>
        <el-form-item label="招聘ID" prop="recruitmentId">
          <el-input v-model="form.recruitmentId" placeholder="请输入招聘ID" />
        </el-form-item>
        <el-form-item label="简历内容">
          <editor v-model="form.resumeContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="社团审核人" prop="clubAuditorId">
          <el-input v-model="form.clubAuditorId" placeholder="请输入社团审核人" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
            v-model="form.auditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApplication, getApplication, delApplication, addApplication, updateApplication } from "@/api/club/application"
import { listUser } from "@/api/system/user"
import { listInfo } from "@/api/club/info"

export default {
  name: "Application",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社团报名申请表格数据
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        clubId: null,
        recruitmentId: null,
        resumeContent: null,
        status: null,
        clubAuditorId: null,
        applyTime: null,
        auditTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "申请人ID不能为空", trigger: "blur" }
        ],
        clubId: [
          { required: true, message: "社团ID不能为空", trigger: "blur" }
        ],
        recruitmentId: [
          { required: true, message: "招聘ID不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态：0待审核，1通过，2拒绝不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "更新者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      },
      // 显示名称映射
      userMap: {},
      clubMap: {},
    }
  },
  created() {
    this.getList()
    this.loadUserMap()
    this.loadClubMap()
  },
  methods: {
    statusText(status) {
      if (status === 1) return '通过'
      if (status === 2) return '拒绝'
      return '待审核'
    },
    loadUserMap() {
      const query = { pageNum: 1, pageSize: 999 }
      listUser(query).then(res => {
        const rows = res.rows || []
        const map = {}
        rows.forEach(u => { map[u.userId] = u.nickName || u.userName || ("用户" + u.userId) })
        this.userMap = map
      }).catch(() => {
        this.userMap = {}
      })
    },
    loadClubMap() {
      const query = { pageNum: 1, pageSize: 999 }
      listInfo(query).then(res => {
        const rows = res.rows || []
        const map = {}
        rows.forEach(c => { map[c.id] = c.name || ("社团" + c.id) })
        this.clubMap = map
      }).catch(() => {
        this.clubMap = {}
      })
    },
    getUserName(id) {
      if (id == null) return ''
      return this.userMap[id] || id
    },
    getClubName(id) {
      if (id == null) return ''
      return this.clubMap[id] || id
    },
    statusTagType(status) {
      if (status === 1) return 'success'
      if (status === 2) return 'danger'
      return 'warning'
    },
    /** 查询社团报名申请列表 */
    getList() {
      this.loading = true
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        clubId: null,
        recruitmentId: null,
        resumeContent: null,
        status: null,
        clubAuditorId: null,
        applyTime: null,
        auditTime: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加社团报名申请"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getApplication(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改社团报名申请"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplication(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addApplication(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除社团报名申请编号为"' + ids + '"的数据项？').then(function() {
        return delApplication(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    statusText(status) {
      if (status === 1) return '通过'
      if (status === 2) return '拒绝'
      return '待审核'
    },
    statusTagType(status) {
      if (status === 1) return 'success'
      if (status === 2) return 'danger'
      return 'warning'
    },
    changeStatus(row, status) {
      const text = status === 1 ? '允许' : '拒绝'
      const d = new Date()
      const pad = n => (n < 10 ? '0' + n : '' + n)
      const now = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
      this.$modal
        .confirm(`是否将申请人“${row.userId}”的申请状态设置为${text}？`)
        .then(() => {
          return updateApplication({ id: row.id, status: status, auditTime: now })
        })
        .then(() => {
          this.$modal.msgSuccess('状态已更新')
          this.getList()
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('club/application/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
